//
//  ChaseBookViewController.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/3.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "ChaseBookViewController.h"
#import "MyViewController.h"
#import "WLScrollView.h"
#import "CycleScrollView.h"
#import "ChaseBookHeadView.h"
#import "JDKJAlertView.h"
#import "LSYReadPageViewController.h"
#import "DCPageVC.h"

@interface ChaseBookViewController () <WLScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

//滚动图
@property (nonatomic , weak)ChaseBookHeadView *headView;

@end

@implementation ChaseBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"";
    
    //左侧
    [self showLeftTitleWithTitle:@"我的书架" withColor:CSWhiteColor];
    
    //右侧
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 33, 33)];
    bgView.backgroundColor = CSWhiteColor;
    bgView.layer.cornerRadius = 16.5;
    bgView.clipsToBounds = YES;
    
  
    UIButton *personBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    personBtn.frame = CGRectMake(1.5, 1.5, 30, 30);
    [personBtn setImage:@"未登录头像新"];
    [personBtn setTitleColor:CSWhiteColor forState:UIControlStateNormal];
    [personBtn addTarget:self action:@selector(personCenter) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:personBtn];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:bgView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.dataArr addObject:@""];
    [self.dataArr addObject:@""];[self.dataArr addObject:@""];
    [self.dataArr addObject:@""];
    [self.dataArr addObject:@""];
    
    [self tableView];
    
    
    
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self shopwTipView];
}



#pragma mark - 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = [UIColor clearColor];
        
        tableView.estimatedRowHeight = 150;
        tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:tableView];
        
        
        
        
        //头部
        ChaseBookHeadView *headView = [[ChaseBookHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150)];
        
        [self.view addSubview:headView];
        self.headView = headView;
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.bottom.width.mas_equalTo(self.view);
            make.top.mas_equalTo(headView.mas_bottom);
            
        }];
        
        //底部
        ChaseBookfToolView *toolView = [[ChaseBookfToolView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200)];
        tableView.tableFooterView = toolView;
        [toolView addTarget:self action:@selector(addBook)];
        
        //注册
        [tableView registerClass:[ChaseBookcell class] forCellReuseIdentifier:@"cell"];
        
        
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count ;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ChaseBookcell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = 0;
    [cell.delBtn addTarget:self action:@selector(delete:)];
    cell.delBtn.tag = indexPath.row + 100;
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LSYReadPageViewController *pageView = [[LSYReadPageViewController alloc] init];
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"郭黄之恋"withExtension:@"txt"];
    pageView.resourceURL = fileURL;    //文件位置
    pageView.model = [LSYReadModel getLocalModelWithURL:fileURL];  //阅读模型
    [self.navigationController pushViewController:pageView animated:YES];
    
}

#pragma mark - 删除
-(void)delete:(UIButton *)sender
{
    WS(weakSelf);
    JDKJAlertView *alertView = [[JDKJAlertView alloc]init];
    [alertView showWithAnimation];
    
    alertView.leftClick = ^{
        
        [weakSelf.dataArr removeObjectAtIndex:sender.tag - 100];
        [weakSelf.tableView reloadData];
        
        [weakSelf shopwTipView];
        
        
    };
    
    
}

-(void)shopwTipView
{
    
    WS(weakSelf);
    if (weakSelf.dataArr.count == 0) {
        weakSelf.noDataView = [NoDataView shareInstance];
        [weakSelf.noDataView showTopInView:weakSelf.view];
        
        [weakSelf.noDataView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(weakSelf.headView.mas_bottom);
            make.left.width.bottom.mas_equalTo(weakSelf.view);
        }];
        
        [weakSelf.noDataView.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(weakSelf.noDataView);
            
            make.centerY.mas_equalTo(weakSelf.noDataView.mas_centerY).offset(-50);
        }];
        
        weakSelf.noDataView.noticeLabel.text = @"没有任何内容喔";
        
        
        
        /**
         添加按钮
         */
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setTitle:@"去添加" forState:UIControlStateNormal];
        [addBtn setTitleColor:CSColorFromRGB(0xF52C50) forState:UIControlStateNormal];
        addBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        addBtn.layer.borderWidth = 2;
        addBtn.layer.borderColor = CSColorFromRGB(0xF52C50).CGColor;
        [addBtn addTarget:weakSelf action:@selector(addBook)];
        addBtn.layer.cornerRadius = 5;
        addBtn.clipsToBounds = YES;
        [weakSelf.noDataView addSubview:addBtn];
        
        [addBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(weakSelf.noDataView);
            make.top.mas_equalTo(weakSelf.noDataView.noticeLabel.mas_bottom).offset(20);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(120);
            
        }];
        
        weakSelf.tableView.hidden = YES;
    }else
    {
        weakSelf.tableView.hidden = NO;
    }
}

#pragma mark - 添加
-(void)addBook
{
    
    //手动跳转到我的页面
    [self.tabBarController setSelectedIndex:2];
    
//    [self.dataArr addObject:@""];
//    [self.tableView reloadData];
//    [self shopwTipView];
}

#pragma mark - 个人中心跳转

-(void)personCenter
{
    MyViewController *myVC = [[MyViewController alloc]init];
    [self.navigationController pushViewController:myVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
